package com.goodflower.goodflower;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.signature.StringSignature;
import com.goodflower.goodflower.info.Info;

import java.util.List;
import java.util.UUID;

public class FlowerItemAdapter extends RecyclerView.Adapter<FlowerItemAdapter.ViewHolder> {
    private List<FlowerItem> mflowerItemList;
    private Context context;

    public FlowerItemAdapter(List<FlowerItem> mflowerItemList) {
        this.mflowerItemList = mflowerItemList;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        CardView cardView;
        TextView flowerItemText;
        ImageView flower_item_image;
        public ViewHolder(View itemView) {
            super(itemView);
            cardView = (CardView) itemView;
            flowerItemText = (TextView) itemView.findViewById(R.id.flower_name);
            flower_item_image = (ImageView)itemView.findViewById(R.id.flower_image);
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        context = parent.getContext();
        View view = View.inflate(context, R.layout.flower_item, null);
        final ViewHolder viewHolder = new ViewHolder(view);
        viewHolder.cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, FLowerInfo.class);
                int adapterPosition = viewHolder.getAdapterPosition();
                FlowerItem flowerItem = mflowerItemList.get(adapterPosition);
                intent.putExtra("flowername",flowerItem.getFlower_name());
                intent.putExtra("username",flowerItem.getFlower_username());
                intent.putExtra("mac",flowerItem.getFlower_mac());
                intent.putExtra("id",flowerItem.getFlower_id());
                Log.d("chuansong","数值>"+flowerItem.getFlower_id());
                context.startActivity(intent);
            }
        });
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        FlowerItem flowerItem = mflowerItemList.get(position);
        holder.flowerItemText.setText(flowerItem.getFlower_name());
        Glide.with(context).load("http://"+ Info.server_ip+"/huahua/floweruser/"+flowerItem.getFlower_username()+"/"+flowerItem.getFlower_mac()+"/"+flowerItem.getFlower_id()+".jpg").signature(new StringSignature(UUID.randomUUID().toString())).into(holder.flower_item_image);
    }

    @Override
    public int getItemCount() {
        return mflowerItemList.size();
    }
}
